package com.ideal.view.component;

import com.ideal.view.form.TemplateConfigForm;
import com.ideal.view.settings.TemplateBaseInfo;
import com.ideal.view.settings.TemplateSetting;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.options.SearchableConfigurable;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * 模板位置
 * @author
 */
public class TemplateConfiguration implements ApplicationComponent, SearchableConfigurable {

    private TemplateConfigForm configForm;

    private TemplateSetting setting = TemplateSetting.getInstance();

    public TemplateConfiguration(){

    }

    @NotNull
    @Override
    public String getId() {
        return "TemplateConfiguration";
    }

    @Nls
    @Override
    public String getDisplayName() {
        return this.getId();
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return null;
    }

    @Nullable
    @Override
    public JComponent createComponent(){
        if(this.configForm==null){
            this.configForm = new TemplateConfigForm();
        }
        return this.configForm.getRootComponent();
    }

    @Override
    public boolean isModified(){
        return configForm.isModified(this);
    }

    @Override
    public void apply(){
        if(this.configForm!=null){
            TemplateBaseInfo info = new TemplateBaseInfo();
            info.setController(configForm.controller.getText());
            info.setService(configForm.service.getText());
            info.setServiceImpl(configForm.serviceimpl.getText());
            info.setDao(configForm.dao.getText());
            info.setDaoImpl(configForm.daoimpl.getText());
            info.setDomain(configForm.domain.getText());
            info.setDomainMapping(configForm.domain_hbm_xml.getText());

            info.setDriverClass(configForm.driverClass.getText());
            info.setJdbcUrl(configForm.jdbcUrl.getText());
            info.setUser(configForm.username.getText());
            info.setPassword(configForm.password.getText());
            info.setForm(configForm.Form.getText());
            this.setting.setInfo(info);
        }
    }
    @Override
    public void reset(){
        if(this.configForm!=null){

            this.configForm.controller.setText(setting.getInfo().getController());
            this.configForm.service.setText(setting.getInfo().getService());
            this.configForm.serviceimpl.setText(setting.getInfo().getServiceImpl());
            this.configForm.dao.setText(setting.getInfo().getDao());
            this.configForm.daoimpl.setText(setting.getInfo().getDaoImpl());
            this.configForm.domain.setText(setting.getInfo().getDomain());
            this.configForm.domain_hbm_xml.setText(setting.getInfo().getDomainMapping());

            this.configForm.jdbcUrl.setText(setting.getInfo().getJdbcUrl());
            this.configForm.username.setText(setting.getInfo().getUser());
            this.configForm.password.setText(setting.getInfo().getPassword());

            this.configForm.Form.setText(setting.getInfo().getForm());
        }
    }
}
